﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
using AppToolKit.Core.Context.Spring;
using CenterDateAchieve.Forms;
using BLL.Action.Job;
using BLL.Action.Core.State;
 


namespace CenterDateAchieve
{
    public partial class MainForm :FormBase
    {
        public MainForm()
        {
            InitializeComponent();
        }
        public delegate void DelegateOnStop();
        [AutoWiring]
        public JobFaced JobFacedHolder { get; set; }
        [AutoWiring]
        public StateMonitor StateMonitorHolder { get; set; }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LogForm log = new LogForm();
            log.MdiParent = this;
            log.ControlBox = false;
           
            
            log.Show();

            StateMonitorHolder.threadCountEvent += new EventHandler(StateMonitorHolder_threadCountEvent);
            StateMonitorHolder.threadErrorEvent += new EventHandler(StateMonitorHolder_threadErrorEvent);
            StateMonitorHolder.RunMonitor();
        }

        void StateMonitorHolder_threadErrorEvent(object sender, EventArgs e)
        {
            this.lb_Error.Text = string.Format("错误{0}", Convert.ToInt32(sender));
        }

        void StateMonitorHolder_threadCountEvent(object sender, EventArgs e)
        {
            this.lb_Run.Text = string.Format("运行{0}", Convert.ToInt32(sender));
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            btStop_Click(sender, e);
            this.Close();
        }

        private void btSetting_Click(object sender, EventArgs e)
        {
            TaskListForm form = new TaskListForm();
            ContextAwareHolder.AutowireObject(form);
            form.StartPosition = FormStartPosition.CenterScreen;
            form.ShowDialog();
        }

        private void btManual_Click(object sender, EventArgs e)
        {
            HandTransferForm form = new HandTransferForm();
            ContextAwareHolder.AutowireObject(form);
            form.StartPosition = FormStartPosition.CenterScreen;
            form.ShowDialog();

        }

        private void btStart_Click(object sender, EventArgs e)
        {
            btStart.Visible = false;
            btStop.Visible = true;
            MenuItemStart.Enabled = false;
            MenuItemStop.Enabled = true;

            JobFacedHolder.AutoRun();
        }

        private void btStop_Click(object sender, EventArgs e)
        {
            btStart.Visible = true;
            btStop.Visible = false;
            MenuItemStart.Enabled =true ;
            MenuItemStop.Enabled = false;
            DelegateOnStop Onstop = new DelegateOnStop(JobFacedHolder.AutoStop);
            Onstop.BeginInvoke(OnstopCallBack,Onstop);
        }
        public void OnstopCallBack(IAsyncResult ar)
        {
            DelegateOnStop Onstop = ar.AsyncState as DelegateOnStop ;
            Onstop.EndInvoke(ar);
        }

        private void ewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TaskListForm form = new TaskListForm();
            ContextAwareHolder.AutowireObject(form);
            form.StartPosition = FormStartPosition.CenterScreen;
            form.ShowDialog();

        }

        private void lb_Error_Click(object sender, EventArgs e)
        {

        }
    }
}
